/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import bookmaker.HibernateUtil;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author Maciek
 */
public class Betslip {
    private boolean selected;
    //private int option;//co się wybrało 1x czy 2
    private String stringSelection = null;//musi byc bo bez tego nie moze sie odwołac do funkcji
    private Session session;
    private long id;
    private String selection;
    private double odds;
    private double stake;
    private boolean pending;
    private boolean won;
    private Market market;
    private Bettor bettor;
    private String matchName;
    private String marketName;
    private boolean rendered = false;
    private boolean autoCentre = false;

    public String dodaj() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            this.setBettor((Bettor) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bettor"));
            this.setMarket(((TreeBean)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("treeDemo")).getMarket());
            this.pending = true;
            this.setWon(false);
            Transact tr = new Transact();
            tr.setCredit(stake);
            tr.withdraw();
            getSession().save(this);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return "ok";
    }
    
    public void marketSelected(ActionEvent event) {
        // get employee id
        setSelection(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("market"));
        Market m = ((TreeBean)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("treeDemo")).getMarket();
        if (getSelection().equals("1"))
            setOdds(m.getOdds_home());
        else if (getSelection().equals("2"))
            setOdds(m.getOdds_away());
        else if (getSelection().equals("x"))
            setOdds(m.getOdds_draw());

        setSelected(true);
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the selection
     */
    public String getSelection() {
        return selection;
    }

    /**
     * @param selection the selection to set
     */
    public void setSelection(String selection) {
        this.selection = selection;
    }

    /**
     * @return the odds
     */
    public double getOdds() {
        return odds;
    }

    /**
     * @param odds the odds to set
     */
    public void setOdds(double odds) {
        this.odds = odds;
    }

    /**
     * @return the stake
     */
    public double getStake() {
        return stake;
    }

    /**
     * @param stake the stake to set
     */
    public void setStake(double stake) {
        this.stake = stake;
    }

    /**
     * @return the market
     */
    public Market getMarket() {
        return market;
    }

    /**
     * @param market the market to set
     */
    public void setMarket(Market market) {
        this.market = market;
    }

    /**
     * @return the rendered
     */
    public boolean isRendered() {
        return rendered;
    }

    /**
     * @param rendered the rendered to set
     */
    public void setRendered(boolean rendered) {
        this.rendered = rendered;
    }

    /**
     * @return the autoCentre
     */
    public boolean isAutoCentre() {
        return autoCentre;
    }

    /**
     * @param autoCentre the autoCentre to set
     */
    public void setAutoCentre(boolean autoCentre) {
        this.autoCentre = autoCentre;
    }

    public void toggleDraggable(ActionEvent event) {
        rendered = !this.rendered;
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getStringSelection(){
        if (getSelection().equals("1"))
            return "home";
        else if (getSelection().equals("2"))
            return "away";
        else if (getSelection().equals("x"))
            return "draw";
        return "";
    }

    /**
     * @return the bettor
     */
    public Bettor getBettor() {
        return bettor;
    }

    /**
     * @param bettor the bettor to set
     */
    public void setBettor(Bettor bettor) {
        this.bettor = bettor;
    }

    /**
     * @return the pending
     */
    public boolean isPending() {
        return pending;
    }

    /**
     * @param pending the pending to set
     */
    public void setPending(boolean pending) {
        this.pending = pending;
    }

    /**
     * @return the matchName
     */
    public String getMatchName() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createSQLQuery("select name from game g, market m where g.id=m.gameID and m.id="+market.getId());
            ArrayList list = (ArrayList) q.list();
            matchName = (list.get(0).toString());
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return matchName;
    }

    /**
     * @return the marketName
     */
    public String getMarketName() {try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createSQLQuery("select name from specific_market sm, market m where sm.id=m.specific_marketID and m.id="+market.getId());
            ArrayList list = (ArrayList) q.list();
            marketName = (list.get(0).toString());
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return marketName;
    }

    public String trySubmit(){
        double balance = ((Bettor)(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bettor"))).getBalance();
        if(balance < stake)
            return "error";
        return "submit";
    }

    /**
     * @return the won
     */
    public boolean isWon() {
        return won;
    }

    /**
     * @param won the won to set
     */
    public void setWon(boolean won) {
        this.won = won;
    }
}
